package common

import (
	"encoding/json"

	"github.com/spf13/viper"
)

type WechatAccessToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
}

type WechatPhone struct {
	Errcode   string `json:"errcode"`
	Errmsg    int    `json:"errmsg"`
	PhoneInfo PhoneInfo
}

type WechatPhoneInfo struct {
	PhoneNumber string `json:"phoneNumber"`
}

func GetWechatOpenid(code string) (string, string) {
	url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + viper.GetString("wechat.appId") + "&secret=" + viper.GetString("wechat.secret") + "&code=" + code + "&grant_type=authorization_code"
	data := HttpGet(url)
	var tmpresult map[string]interface{}
	json.Unmarshal([]byte(data), &tmpresult)
	return tmpresult["openid"].(string), tmpresult["access_token"].(string)
}

func GetWechatInfo(openid string, access_token string) map[string]interface{} {
	url := "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN"
	data := HttpGet(url)
	var tmpresult map[string]interface{}
	json.Unmarshal([]byte(data), &tmpresult)
	return tmpresult
}
